<template>
  <div class="resource container">
    <div class="search-box">
      <el-row :gutter="20">
        <el-form label-width="100px" size="small" class="searchForm">
          <el-col :span="6">
            <el-form-item label="订单类型:">
              <el-select
                v-model="params.typeName"
                placeholder="选择订单类型"
                clearable
                @change="search"
              >
                <el-option
                  v-for="item in typeList"
                  :key="item.id"
                  :label="item.title"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="订单内容:">
              <el-input
                v-model="params.typeValue"
                clearable
                @clear="search"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-button
              @click="search"
              icon="el-icon-search"
              size="mini"
              type="primary"
              >搜索</el-button
            >
            <el-button
              @click="cancelData"
              icon="el-icon-refresh"
              size="mini"
              type="warning"
              >重置</el-button
            >
          </el-col>
        </el-form>
      </el-row>
    </div>

    <div>
      <div class="handle-box">
        <el-button
          type="danger"
          icon="el-icon-delete"
          class="handle-del mr10"
          size="mini"
          @click="delAllSelection"
          :disabled="isOperate"
          >批量删除</el-button
        >
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >添加
        </el-button>
      </div>

      <el-table
        v-loading="loading"
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="55"
          align="center"
        ></el-table-column>
        <el-table-column label="订单类型" min-width="150" align="center">
          <template slot-scope="scope">
            {{ formatType(scope.row.typeName) }}
          </template>
        </el-table-column>
        <el-table-column label="订单内容" min-width="150" align="center">
          <template slot-scope="scope">
            {{ scope.row.typeValue }}
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" min-width="160">
          <template slot-scope="scope">{{ scope.row.createTime }}</template>
        </el-table-column>
        <el-table-column
          label="操作"
          min-width="180"
          align="center"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="handleView(scope.row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="reject(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <pagination
          v-show="pageTotal > 0"
          :total="pageTotal"
          :page.sync="params.pageNum"
          :limit.sync="params.pageSize"
          @pagination="getData00"
        />
      </div>
    </div>

    <!-- 添加/编辑 -->
    <el-dialog :title="title666" :visible.sync="dialogVisible" width="700px">
      <el-form
        :rules="rules1"
        ref="typeForm"
        :model="formData"
        label-width="100px"
        size="small"
        class="form-box"
      >
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="订单类型" prop="typeName">
              <el-select v-model="formData.typeName" placeholder="选择订单类型">
                <el-option
                  v-for="item in typeList"
                  :key="item.id"
                  :label="item.title"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="订单内容" prop="typeValue">
              <el-input v-model="formData.typeValue"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="sure" type="primary" size="small">确定</el-button>
        <el-button @click="cancelSave" type="danger" plain size="small"
          >取消</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  orderTypeList,
  addType,
  updateType,
  deleteType
} from "@/api/systemSettings/order";

// import logDialog from "./logDialog.vue";
export default {
  //   components: { logDialog },
  props: [],
  data() {
    return {
      loading: true,
      //数据
      tableData: [],
      rules1: {
        typeName: [
          { required: true, message: "请选择订单类型", trigger: "change" }
        ],
        typeValue: [
          { required: true, message: "请填写类型值", trigger: "blur" }
        ]
      },
      typeList: [
        {
          id: "1",
          title: "系统订单"
        },
        {
          id: "2",
          title: "组件订单"
        }
      ],

      params: {
        pageNum: 1,
        pageSize: 10,
        typeName: "",
        typeValue: ""
      },
      pageTotal: 0,
      //删除
      multipleSelection: [],

      formData: {},
      dialogVisible: false,
      title666: "",

      isOperate: true
    };
  },
  computed: {},
  created() {
    this.getData00();
  },
  filters: {
    formatDate: function(value) {
      let date = new Date(parseInt(value) * 1000);
      let y = date.getFullYear();
      let m = date.getMonth() + 1;
      m = m < 10 ? "0" + m : m;
      let d = date.getDate();
      d = d < 10 ? "0" + d : d;
      let h = date.getHours();
      h = h < 10 ? "0" + h : h;
      let minute = date.getMinutes();
      let second = date.getSeconds();
      minute = minute < 10 ? "0" + minute : minute;
      second = second < 10 ? "0" + second : second;
      return y + "-" + m + "-" + d + " " + h + ":" + minute + ":" + second;
    }
  },
  mounted() {},
  watch: {},
  directives: {
    "el-select-loadmore": {
      bind(el, binding) {
        var _this = this;
        const SELECTWRAP_DOM = el.querySelector(
          ".el-select-dropdown .el-select-dropdown__wrap"
        );
        SELECTWRAP_DOM.addEventListener("scroll", function() {
          const condition =
            this.scrollHeight - this.scrollTop - 1 <= this.clientHeight;
          if (condition) {
            binding.value();
          }
        });
      }
    }
  },
  methods: {
    // 点击添加
    handleAdd() {
      this.formData = {};
      this.dialogVisible = true;
      this.title666 = "新增";
    },
    // 点击保存
    sure() {
      this.$refs["typeForm"].validate(valid => {
        if (valid) {
          if (this.title666 == "新增") {
            addType(this.formData).then(res => {
              if (res.code == 200) {
                this.$message.success(`新增成功`);
                this.dialogVisible = false;
                this.getData00();
              }
            });
          } else {
            updateType(this.formData).then(res => {
              if (res.code == 200) {
                this.$message.success(`修改成功`);
                this.dialogVisible = false;
                this.getData00();
              }
            });
          }
        }
      });
    },
    // 点击取消
    cancelSave() {
      this.dialogVisible = false;
      this.formData = {};
      this.getData00();
    },
    // 点击编辑
    handleView(row) {
      this.dialogVisible = true;
      this.title666 = "修改";
      this.formData = row;
    },

    loadmore() {
      this.transferPeopleParams.pageNum++;
      this.getList();
    },

    cancelData() {
      this.params = {
        pageNum: 1,
        pageSize: 10,
        typeName: "",
        typeValue: ""
      };
      this.getData00();
    },

    formatType(val) {
      var str = this.typeList.find(function(item) {
        return item.id == val;
      });
      if (str) return str.title;
    },

    // 批量操作
    handleSelectionChange(val) {
      if (val.length != 0) {
        this.multipleSelection = val.map(item => item.id);
        this.isOperate = false;
      } else {
        this.isOperate = true;
      }
    },

    // 批量拒绝
    delAllSelection() {
      var str = this.multipleSelection.join(",");
      this.$confirm("确定要删除吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.deleteData(str);
          this.multipleSelection = [];
        })
        .catch(() => {});
    },
    reject(row) {
      this.$confirm("确定要删除吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.deleteData(row.id);
        })
        .catch(() => {});
    },
    deleteData(id) {
      deleteType(id)
        .then(res => {
          if (res.code == 200) {
            this.$message.success("删除成功");
            this.getData00();
            this.isOperate = true;
          } else {
            this.$message.error("删除失败");
          }
        })
        .catch(() => {
          console.log("error submit!!");
          return false;
        });
    },
    //获取数据
    getData00() {
      var _this = this;
      let data = this.params;
      this.loading = false;
      orderTypeList(data)
        .then(res => {
          if (res.code == 200) {
            this.tableData = res.rows;
            this.pageTotal = res.total;
            this.loading = false;
          }
        })
        .catch(() => {
          console.log("error submit!!");
          return false;
        });
    },

    search() {
      this.$set(this.params, "pageNum", 1);
      this.getData00();
    }
  }
};
</script>

<style>
.timeDate .el-date-editor {
  width: 50%;
  float: left;
}
</style>
<style scoped>
.container {
  padding: 5px 20px !important;
}
.handle-box {
  margin-bottom: 10px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.yellow {
  color: #f57506;
}
.green {
  color: green;
  cursor: pointer;
}
.blue {
  color: #409eff;
  cursor: pointer;
  text-decoration: underline;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
.search-box {
  margin-bottom: 10px;
  padding: 10px 10px 10px 10px;
  border-radius: 4px;
  box-sizing: border-box;
  background: #fff;
  border: 1px solid #ddd;
  border-radius: 5px;
}
.searchForm .el-form-item--mini.el-form-item,
.searchForm .el-form-item--small.el-form-item {
  margin-bottom: 1px;
}
/* .search-box .el-input {
  width: 200px;
  margin-right: 10px;
} */
</style>
